package com.ymatou.userregister.testcase;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.Gson;
import com.ymatou.userregister.service.SendActiveCodeToEmailCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.sqlwapper.YmtValidationCodeWapper;
import com.ymttest.utils.TestDataManager;

public class Ts_SendActiveCodeToEmail {
	private static SendActiveCodeToEmailCall sendActiveCodeToEmailCall;
	private YmtValidationCodeWapper validateCodeWapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送激活码到邮箱");
	}

	@Before
	public void caseUp() throws Exception {
		sendActiveCodeToEmailCall = new SendActiveCodeToEmailCall();
		validateCodeWapper = new YmtValidationCodeWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendActiveCodeToEmailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SendActiveCodeToEmail_001() {
		Logger.start(true,"成功发送激活码到邮箱");
		try {
			String email = TestDataManager.getPhoneNum()+"@emailactive.com";
			Map user = UserCallService.registerUser(email, TestDataManager.getPhoneNum(),null);
			
			Map params = new HashMap<String, Object>();
			params.put("lId", user.get("userId"));
			params.put("tomail", email);
			params.put("uId", user.get("loginId"));
			
			sendActiveCodeToEmailCall.setData(new Gson().toJson(params));
			sendActiveCodeToEmailCall.callService();
			
			Thread.sleep(1000);
			String validateCode = validateCodeWapper.getValidateCode(email);
			Logger.verifyNotNull(validateCode, "验证验证码已存入数据库");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SendActiveCodeToEmail_002() {
		Logger.start(false,"未输入邮箱，失败");
		try {
			String email = TestDataManager.getPhoneNum()+"@emailactive.com";
			Map user = UserCallService.registerUser(email, TestDataManager.getPhoneNum(),null);
			
			Map params = new HashMap<String, Object>();
			params.put("lId", user.get("userId"));
			params.put("uId", user.get("loginId"));
			
			sendActiveCodeToEmailCall.setData(new Gson().toJson(params));
			sendActiveCodeToEmailCall.callService();
			
			Thread.sleep(1000);
			String validateCode = validateCodeWapper.getValidateCode(email);
			Logger.verifyIsNull(validateCode, "验证验证码未存入数据库");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SendActiveCodeToEmail_003() {
		Logger.start(true,"未输入用户Id, 成功");
		try {
			String email = TestDataManager.getPhoneNum()+"@emailactive.com";
			Map user = UserCallService.registerUser(email, TestDataManager.getPhoneNum(),null);
			
			Map params = new HashMap<String, Object>();
			params.put("tomail", email);
			params.put("uId", user.get("loginId"));
			
			sendActiveCodeToEmailCall.setData(new Gson().toJson(params));
			sendActiveCodeToEmailCall.callService();
			
			Thread.sleep(1000);
			String validateCode = validateCodeWapper.getValidateCode(email);
			Logger.verifyNotNull(validateCode, "验证验证码已存入数据库");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SendActiveCodeToEmail_004() {
		Logger.start(true,"未输入登陆Id, 成功");
		try {
			String email = TestDataManager.getPhoneNum()+"@emailactive.com";
			Map user = UserCallService.registerUser(email, TestDataManager.getPhoneNum(),null);
			
			Map params = new HashMap<String, Object>();
			params.put("lId", user.get("userId"));
			params.put("tomail", email);
			
			sendActiveCodeToEmailCall.setData(new Gson().toJson(params));
			sendActiveCodeToEmailCall.callService();
			
			Thread.sleep(1000);
			String validateCode = validateCodeWapper.getValidateCode(email);
			Logger.verifyNotNull(validateCode, "验证验证码已存入数据库");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SendActiveCodeToEmail_005() {
		Logger.start(false,"输入错误格式的邮箱, 失败");
		try {
			String email = TestDataManager.getRandomNum(5);
			
			Map params = new HashMap<String, Object>();
			params.put("lId", -1);
			params.put("tomail", email);
			
			sendActiveCodeToEmailCall.setData(new Gson().toJson(params));
			sendActiveCodeToEmailCall.callService();
			
			Thread.sleep(1000);
			String validateCode = validateCodeWapper.getValidateCode(email);
			Logger.verifyNotNull(validateCode, "验证验证码存入数据库");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
